﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using vHCS.HR.Entities;
using Core.ExceptionHandling;
using Core.Message.Entities;
using Core.Controls.Message;
using Core.Cache;
using System.Data;

namespace vHCS.Web.MdlHR.Controls
{
    /// <summary>
    /// HungDV
    /// </summary>
    public partial class HR_ctrQuestions : System.Web.UI.UserControl
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        /// <summary>
        /// Nhóm validate
        /// </summary>
        public string ValidationGroup
        {
            get
            {
                return this.hdfValidationGroup.Value;
            }
            set
            {
                this.hdfValidationGroup.Value = value;

            }
        }

        #region DataBinding
       
        /// <summary>
        /// Danh sách câu hỏi theo dạng chọn
        /// Trả về danh sách câu hỏi và kết quả trả lời của user
        /// </summary>
        public List<HR_QuestionDTO> SelectQuestions {
            set {               
                this.dtlQuestions.DataSource = value;
            }
            get {
                List<HR_QuestionDTO> questions = new List<HR_QuestionDTO>();
                foreach (DataListItem item in dtlQuestions.Items)
                {
                    HR_ctrAnswers answer = (HR_ctrAnswers)item.FindControl("ctrAnswer");
                    if (answer != null)
                        questions.Add(answer.Question);                    
                }
                return questions;
            }
            
        }
        /// <summary>
        /// Danh sách câu hỏi theo dạng Textbox
        /// Trả về danh sách câu hỏi và kết quả trả lời của user
        /// </summary>
        public List<HR_QuestionDTO> TextboxQuestions
        {
            set
            {
                this.dtlQuestions.DataSource = value;
            }
            get
            {
                List<HR_QuestionDTO> questions = new List<HR_QuestionDTO>();
                foreach (DataListItem item in dtlQuestions.Items)
                {
                    HR_ctrAnswers answer = (HR_ctrAnswers)item.FindControl("ctrAnswer");
                    
                    if (answer != null)
                        questions.Add(answer.Question);
                }
                return questions;
            }

        }

        /// <summary>
        /// Danh sách câu hỏi theo dạng table
        /// </summary>
        public List<HR_QuestionDTO> TableQuestions
        {
            set
            {
                this.dtlTableAnswerList.DataSource = value;
                if (value != null && value.Count > 0) 
                    this.pnlTableAnswer.Visible = true;
                else 
                    this.pnlTableAnswer.Visible = false;
            }
            get { 
                 List<HR_QuestionDTO> questions = new List<HR_QuestionDTO>();
                 foreach (DataListItem item in this.dtlTableAnswerList.Items)
                 {
                     TextBox txtQuestionId = (TextBox)item.FindControl("hdfQuestionId");
                     TextBox txtMarks = (TextBox)item.FindControl("txtMarks");
                     TextBox txtComment = (TextBox)item.FindControl("txtComment");
                     HR_QuestionDTO question = new HR_QuestionDTO();
                     question.SelectType = QuestionsSelectType.Table;
                     question.InputType = QuestionsInputType.None;
                     question.QuestionID = long.Parse(txtQuestionId.Text);
                     question.Marks = txtMarks.Text.Trim().ToUpper();
                     question.Comment = txtComment.Text.Trim();
                     questions.Add(question);
                 }
                 return questions;
            }
        } 

        /// <summary>
        /// Bind câu hỏi vào các control
        /// </summary>
        public override void DataBind()
        {
            this.dtlQuestions.DataBind();
            this.dtlTableAnswerList.DataBind();
           
        }
       
        protected void dtlQuestions_ItemDataBound(object sender, DataListItemEventArgs e){
            try
            {
                if (e.Item.ItemType != ListItemType.Header
                    && e.Item.ItemType != ListItemType.Footer
                    && e.Item.ItemType != ListItemType.Pager
                )
                {
                    Label lblTopicName = (Label)e.Item.FindControl("lblTopicName");
                   
                    HR_QuestionDTO objQuestion = (HR_QuestionDTO)e.Item.DataItem;
                    HR_ctrAnswers ctrAnswer = (HR_ctrAnswers)e.Item.FindControl("ctrAnswer");                   
                   
                    //Binding question                   
                    ctrAnswer.Index = e.Item.ItemIndex + 1;
                    ctrAnswer.Question = objQuestion;
                    ctrAnswer.ValidationGroup = this.ValidationGroup;
                    ctrAnswer.DataBind();

                }
            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }
        protected void dtlTableAnswerList_ItemDataBound(object sender, DataListItemEventArgs e) {
            try
            {
                if (e.Item.ItemType != ListItemType.Header
                    && e.Item.ItemType != ListItemType.Footer
                    && e.Item.ItemType != ListItemType.Pager
                )
                {
                    HR_QuestionDTO objQuestion = (HR_QuestionDTO)e.Item.DataItem;
                    RequiredFieldValidator requireValidator = (RequiredFieldValidator)e.Item.FindControl("valRequireMarks");
                    
                    if(objQuestion.IsRequire && requireValidator!=null)
                    {                    
                       requireValidator.ValidationGroup=this.ValidationGroup;
                       requireValidator.Visible=true;
                    }
               

                }
            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }
        
        #endregion
    }
}